<?
if (preg_match("#" . basename(__FILE__) . "#", $_SERVER['PHP_SELF'])) {
    die('This page cannot be called directly.');
}

class ajax_promotion
{
    function playme()
    {

        $code = Url::getParam('code');
        //yêu cầu đặt tên $code=tên của method trong class
        if (method_exists($this, $code)) {
            $this->$code();
        } else {
            $this->home();
        }
    }

    function home()
    {
        global $display;
        die("Nothing to do...");
    }

    function getPromotionByHotel()
    {
        $id = Url::getInt('hotel_id', 0);
        if ($id) {
            if ($pro = Db_Promotion::getPromotionByHotelId($id)) {
                $ret = "";
                foreach ($pro as $p) {
                    $ret .= $p['time_start'] . " => " . $p['time_end'] . " KM " . $p['name_type'] . " giá " . $p['price'] . " (giảm " . $p['percent'] . "%) áp dụng
            cho " . $p['name_room'] . " <a href='manage_promotion_hotel.html?cmd=edit&id=" . $p['id'] . "&href=manage_promotion_hotel.html'>[Sửa]</a><br>";
                }
                echo $ret;
            }
        }
    }

    function getPromotionHotel()
    {
        global $display;
        Db_Location::get_cities();
        $display->add("WEB_ROOT", WEB_ROOT);
        $display->add("WEB_DIR", WEB_DIR);
        $ajax_response = new AjaxRes();

        $city = (Url::getInt("city") ? Url::getInt("city") : 0);
        $star = (Url::getInt("star") ? Url::getInt("star") : 0);
        $special = (Url::getInt("special") ? Url::getInt("special") : 0);

        $pro_filter = Db_Promotion::getPromotionCity('*', '', ' num_promotion_hotel>0 ');
        if (sizeof($pro_filter) > 0) {
            $allprocities = 0;
            foreach ($pro_filter as &$p) {
                $allprocities += $p['num_promotion_hotel'];
                $p['alias'] = String::safe_title($p['name']);
            }
            $display->add('allprocities', $allprocities);
            $display->add('pro_filter', $pro_filter);

            if ($special != 0){
                $pro_hotels = Db_Promotion::getPromotionHotel("*", '', ' status=1 AND time_start<' . TIME_NOW . ' AND time_end>' . TIME_NOW);
            }else{
                $pro_hotels = Db_Promotion::getPromotionHotel("*", '', ' status=1 AND time_start<' . TIME_NOW . ' AND time_end>' . TIME_NOW . '  GROUP BY hotel_id ');
            }
            if (sizeof($pro_hotels) > 0) {
                $hotels = array();
                foreach ($pro_hotels as $p) {
                    $h = Db_Hotel::getHotelById($p['hotel_id']);
                    $h['pro_price'] = $p['price'];
                    $h['pro_percent'] = $p['percent'];

                    if ($city == 0 && $star == 0 && $special == 0) {
                        array_push($hotels, $h);
                    } else if ($city != 0 && $star == 0 && $special == 0) {
                        if ($h['city_id'] == $city) {
                            array_push($hotels, $h);
                        }
                    } else if ($city == 0 && $star != 0 && $special == 0) {
                        if ($h['star'] == $star) {
                            array_push($hotels, $h);
                        }
                    } else if ($city == 0 && $star == 0 && $special != 0) {
                        if ($p['type'] == $special) {
                            array_push($hotels, $h);
                        }
                    } else if ($city != 0 && $star != 0 && $special == 0) {
                        if ($h['city_id'] == $city) {
                            if ($h['star'] == $star) {
                                array_push($hotels, $h);
                            }
                        }
                    } else if ($city == 0 && $star != 0 && $special != 0) {
                        if ($p['type'] == $special) {
                            if ($h['star'] == $star) {
                                array_push($hotels, $h);
                            }
                        }
                    } else if ($city != 0 && $star == 0 && $special != 0) {
                        if ($p['type'] == $special) {
                            if ($h['city_id'] == $city) {
                                array_push($hotels, $h);
                            }
                        }
                    } else if ($city != 0 && $star != 0 && $special != 0) {
                        if ($p['type'] == $special) {
                            if ($h['city_id'] == $city) {
                                if ($h['star'] == $star) {
                                    array_push($hotels, $h);
                                }
                            }
                        }
                    }
                }
                $city = array();
                $imgsDefault = array();
                $hotelsId = array();
                $hotelShow = array();
                foreach ($hotels as $h) {
                    $imgsDefault[] = $h['image_default'];
                    $hotelsId[] = $h['id'];
                    $h['feel'] = Db_HotelReview::getFeelByScore($h['rating_0_0_average']);
                    if ($h['price_min'] != 0) {
                        $hotelShow[] = $h;
                        $city[] = Db_Location::getCityNotDistrict($h['city_id']);
                    }
                }
                if (sizeof($hotelShow) > 0) {
                    if (sizeof($imgsDefault) > 0) {
                        $imgsDefault = join(',', $imgsDefault);
                        $imgs = Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN ($imgsDefault)");
                        $display->add('imgs', $imgs);
                    }
                    $display->add('hotels', $hotelShow);
                    $display->add('city', $city);
                    $result = $display->output('load_promotion', true, 'PromotionHotel');
                    $ajax_response->type = AjaxRes::SUCCESS;
                    $ajax_response->item = $result;
                } else {
                    $ajax_response->type = AjaxRes::SUCCESS;
                    $ajax_response->item = '<br /><strong style="text-align: center; font-size: 16px; padding: 80px; display: block;">Không có khách sạn nào</strong><br />';
                }
            } else {
                $ajax_response->type = AjaxRes::SUCCESS;
                $ajax_response->item = '<br /><strong style="text-align: center; font-size: 16px; padding: 80px; display: block;">Không có khách sạn nào</strong><br />';
            }
            echo $ajax_response->toString();
        }
    }

    /*function getPromotionSpecial()
    {
        global $display;
        Db_Location::get_cities();
        $display->add("WEB_ROOT", WEB_ROOT);
        $display->add("WEB_DIR", WEB_DIR);
        $ajax_response = new AjaxRes();

        $pro_filter = Db_Promotion::getPromotionFilter('', ' parent=4 AND num_hotel>0 ORDER BY pos');
        if (sizeof($pro_filter) > 0) {
            foreach ($pro_filter as &$p) {
                $p['slug'] = String::safe_title($p['name']);
            }
            $display->add('pro_filter', $pro_filter);
            if ($s = Url::get('special')) {
                $display->add('type', $s);
                $type = 'type = ' . $s . ' AND ';
            } else {
                $a = array_keys($pro_filter);
                $display->add('type', $a[0]);
                $type = ' type = ' . $a[0] . ' AND ';
            }

            $pro_hotels = Db_Promotion::getPromotionHotel("*", '', $type . ' status=1 AND time_start<' . TIME_NOW . ' AND time_end>' . TIME_NOW . '  GROUP BY hotel_id ');
            if (sizeof($pro_hotels) > 0) {
                $hotels = array();
                foreach ($pro_hotels as $pro_hotel) {
                    $hotel = Db_Hotel::getHotelById($pro_hotel['hotel_id']);
                    $hotel['pro_price'] = $pro_hotel['price'];
                    $hotel['pro_percent'] = $pro_hotel['percent'];
                    array_push($hotels, $hotel);
                }
                $imgsDefault = array();
                $hotelsId = array();
                $hotelShow = array();
                foreach ($hotels as $h) {
                    $imgsDefault[] = $h['image_default'];
                    $hotelsId[] = $h['id'];
                    $h['feel'] = Db_HotelReview::getFeelByScore($h['rating_0_0_average']);
                    if ($h['price_min'] != 0) {
                        $hotelShow[] = $h;
                        $city[] = CGlobal::$all_cities[$h['city_id']];
                    }
                }
                $imgsDefault = join(',', $imgsDefault);
                $imgs = Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN ($imgsDefault)");
                $display->add('imgs', $imgs);
                $display->add('hotels', $hotelShow);
                $display->add('city', $city);
                $result = $display->output('load_promotion', true, 'PromotionHotel');
                $ajax_response->type = AjaxRes::SUCCESS;
                $ajax_response->item = $result;

            } else {
                $ajax_response->type = AjaxRes::SUCCESS;
                $ajax_response->item = '<br /><strong style="display: block;text-align: center;">Không có khách sạn nào</strong><br />';
            }
            echo $ajax_response->toString();
        }
    }*/

    function getDeal()
    {
        global $display;
        Db_Location::get_cities();
        $display->add("WEB_ROOT", WEB_ROOT);
        $display->add("WEB_DIR", WEB_DIR);
        $ajax_response = new AjaxRes();
        $city = (Url::getInt("city") ? Url::getInt("city") : 0);
        $star = (Url::getInt("star") ? Url::getInt("star") : 0);

        $deal = Db_Deal::getDealHotelAvailable();
        if ($deal && sizeof($deal) > 0) {
            $newdeal = array();
            $imgsDefault = array();
            foreach ($deal as $d) {
                $hotel = Db_Hotel::getHotelById($d['hotel_id']);
                $pr = Db_Location::getParentId($hotel['city_id']);
                $hotel['city_id'] = ($pr == 0) ? $hotel['city_id'] : $pr;
                $i = 0;
                if ($city == 0 && $star == 0) {
                    $i = 1;
                } else if ($city != 0 && $star == 0) {
                    if ($hotel['city_id'] == $city) {
                        $i = 1;
                    }
                } else if ($city == 0 && $star != 0) {
                    if ($hotel['star'] == $star) {
                        $i = 1;
                    }
                } else if ($city != 0 && $star != 0) {
                    if ($hotel['city_id'] == $city) {
                        if ($hotel['star'] == $star) {
                            $i = 1;
                        }
                    }
                }
                if ($i == 1) {
                    $d['num_deal_book'] = $d['num_deal'] - $d['num_deal_book'];
                    $imgsDefault[] = $hotel['image_default'];
                    $d['hotel'] = $hotel;
                    $newdeal[] = $d;
                }
            }
            if (sizeof($newdeal) > 0) {
                if (sizeof($imgsDefault) > 0) {
                    $imgsDefault = join(',', $imgsDefault);
                    $imgs = Db_Image::selectImages('id, img_name, img_url, img_server', '', "id IN ($imgsDefault)");
                    $display->add('imgs', $imgs);
                }
                $display->add('deal', $newdeal);
                $result = $display->output('load_deal', true, 'DealList');
                $ajax_response->type = AjaxRes::SUCCESS;
                $ajax_response->item = $result;
            } else {
                $ajax_response->type = AjaxRes::SUCCESS;
                $ajax_response->item = '<br /><strong style="text-align: center; font-size: 16px; padding: 80px; display: block;">Không có deal nào</strong><br />';
            }
        } else {
            $ajax_response->type = AjaxRes::SUCCESS;
            $ajax_response->item = '<br /><strong style="text-align: center; font-size: 16px; padding: 80px; display: block;">Không có deal nào</strong><br />';
        }
        echo $ajax_response->toString();
    }
}

?>